package com.hand.qt24953.ssmtrain.service.impl;

import com.github.pagehelper.util.StringUtil;
import com.hand.qt24953.ssmtrain.entity.Item;
import com.hand.qt24953.ssmtrain.entity.ItemExample;
import com.hand.qt24953.ssmtrain.mapper.ItemMapper;
import com.hand.qt24953.ssmtrain.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author qiutao
 * @date 2019/7/19
 */
@Service("itemService")
public class ItemServiceImpl implements ItemService {
    @Autowired
    private ItemMapper itemMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<Item> findPage(Item eo, String order) {
        ItemExample itemExample = new ItemExample();
        ItemExample.Criteria criteria = itemExample.createCriteria();
        if (StringUtil.isNotEmpty(eo.getItemCode())){
            criteria.andItemCodeLike(eo.getItemCode());
        }
        if (StringUtil.isNotEmpty(eo.getItemDescription())){
            criteria.andItemDescriptionLike(eo.getItemDescription());
        }
        if (eo.getStartActiveDate() == null){
            if (eo.getEndActiveDate() != null){
                criteria.andEndActiveDateLessThanOrEqualTo(eo.getEndActiveDate());
            }
        } else {
            if (eo.getEndActiveDate() == null){
                criteria.andStartActiveDateGreaterThanOrEqualTo(eo.getStartActiveDate());
            } else {
                criteria.andStartActiveDateBetween(eo.getStartActiveDate(), eo.getEndActiveDate());
            }
        }
        if (StringUtil.isNotEmpty(eo.getItemUom())){
            criteria.andItemUomEqualTo(eo.getItemUom());
        }
        if (eo.getEnabledFlag() != null){
            criteria.andEnabledFlagEqualTo(eo.getEnabledFlag());
        }
        if(StringUtil.isNotEmpty(order)){
            itemExample.setOrderByClause(order);
        }
        return itemMapper.selectByExample(itemExample);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Item detail(Long pk) {
        return itemMapper.selectByPrimaryKey(pk);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Item update(Long id, Item eo) {
        eo.setItemId(id);
        itemMapper.updateByPrimaryKeySelective(eo);
        return itemMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Item create(Item eo) {
        String maxItemCode = itemMapper.getMaxItemCode();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("ITEM");
        if(maxItemCode == null){
            stringBuilder.append(String.format("%010d",1));
        }else {
            stringBuilder.append(String.format("%010d",
                    Long.parseLong(maxItemCode.substring(4))+1));
        }
        if (eo.getEndActiveDate() != null && eo.getStartActiveDate() != null){
            if(eo.getEndActiveDate().before(eo.getStartActiveDate())){
                throw new RuntimeException("结束时间不能小于开始时间");
            }
        }
        eo.setItemCode(stringBuilder.toString());
        Date date = new Date();
        eo.setCreationDate(date);
        eo.setLastUpdateDate(date);
        eo.setObjectVersionNumber(1L);
        eo.setCreatedBy(-1L);
        eo.setLastUpdatedBy(-1L);
        itemMapper.insert(eo);
        return eo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(Long[] pks) {
        ItemExample itemExample = new ItemExample();
        ItemExample.Criteria criteria = itemExample.createCriteria();
        criteria.andItemIdIn(Arrays.asList(pks));
        itemMapper.deleteByExample(itemExample);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(Long pk) {
        itemMapper.deleteByPrimaryKey(pk);
        return true;
    }
}
